package com.qf.echartywm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.echartywm.entity.Dingdan;
import com.qf.echartywm.entity.Product;
import com.qf.echartywm.service.DingdanService;
import com.qf.echartywm.service.ProductService;
import com.qf.echartywm.vo.Barvo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    ProductService productService;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    DingdanService dingdanService;

    @GetMapping("/bar")
    public Barvo getDate(){

        Barvo barvo = new Barvo();
        List<String> categories = new ArrayList<>();
        List<BigDecimal> values = new ArrayList<>();

        QueryWrapper<Dingdan> dingdanQueryWrapper = new QueryWrapper<>();
        dingdanQueryWrapper.select("pid,sum(sale) as sum").groupBy("pid");

        List<Map<String, Object>> maps = dingdanService.listMaps(dingdanQueryWrapper);

        for (Map<String, Object> map : maps) {
            // { pid:1 ,sum:10}
            Integer pid = (Integer) map.get("pid");
            BigDecimal sum = (BigDecimal) map.get("sum");

            QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
            productQueryWrapper.eq("id",pid);
            productQueryWrapper.select("name");

            Product one = productService.getOne(productQueryWrapper);
            String productName = one.getName();

            categories.add(productName);
            values.add(sum);
        }

        barvo.setCategories(categories);
        barvo.setValues(values);

        return barvo;

    }






















}
